import { v4 as uuid } from 'uuid'
import { StdFigureData } from './figure'
import { StdNakedFigureData } from './naked_figure'
import { StdTableDataV2 } from './table'
import { StdBulletData } from './bullet'
import { StdFormulaDataV2 } from './formula'
import { StdTextDataV2 } from './textData'
import { StdExampleV2 } from './example'

export class StdBlock {
  constructor(params) {
    const { blockId, blockType, blockIndex, groupId, dataType = 'paragraph' } = params
    const { referredId, tableData, bulletData, figureData, formulaData, textData, exampleData, abbrData, termData } =
      params
    // referredId 和 referredIndex 是索引页用到的两个变量
    if (referredId) this.referredId = referredId

    this.blockId = blockId || uuid()
    // 结构化块等，内部内容有相同的 groupId
    if (groupId) this.groupId = groupId
    // 内容块类型，1. 术语：terms 2. 缩略语：abbreviation 3. 收藏 collect
    if (blockType) this.blockType = blockType
    // 层级结构，如 1.1，没有可不传
    if (blockIndex) this.blockIndex = blockIndex
    // 数据类型，枚举参考data-type, 值为scope时表示范围的标题
    if (dataType) this.dataType = dataType
    // 表格整体数据 - dataType=table时必须有值
    if (tableData) this.tableData = new StdTableDataV2(tableData)
    // 列项整体数据 - dataType=item_list时必须有值
    if (bulletData) this.bulletData = new StdBulletData(bulletData)
    // 图片整体数据 - dataType=figure时必须有值
    if (figureData) {
      if (dataType === 'figure') this.figureData = new StdFigureData(figureData)
      else if (dataType === 'naked_figure') this.figureData = new StdNakedFigureData(figureData)
    }
    // 公式整体数据 - dataType=formula时必须有值
    if (formulaData) this.formulaData = new StdFormulaDataV2(formulaData)
    // 文本数据 - 除开上述以外的数据类型时有值
    if (textData) this.textData = new StdTextDataV2(textData)
    // 示例数据
    if (exampleData) this.exampleData = new StdExampleV2(exampleData)
    // 缩略语数据
    if (abbrData) this.abbrData = abbrData
    // 术语
    if (termData) this.termData = termData
  }

  setBlockType(type) {
    this.blockType = type
  }

  setGroupId(id) {
    this.groupId = id
  }
}
